import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/collection_can_transfer_page_model.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:oktoast/oktoast.dart';
import 'danation_confirm_widget.dart';

class GoodsModel {
  String number;
  String price;
  RxBool isChoose = false.obs;
  GoodsModel({
    required this.number,
    required this.price,
    required this.isChoose,
  });
}

class DanationChooseGoodsController extends GetxController {
  var number = 0.obs;
  final bottomEnble = false.obs;
  var dataList = <GoodsModel>[].obs;
  // var chooseList = <GoodsModel>[].obs;
  var type = '1'.obs; //1内部转赠 2转至臻藏
  var transferList = RxList<CollectionCanTransferPageItem>([]);
  final scrollController = ScrollController();
  int pageNum = 1;
  int pageSize = 20;
  final collectionId = '0'.obs;
  final url = ''.obs;
  final name = ''.obs;
  final drawerCount = 0.obs;
  final num = 0.obs;
  final selectedList = [].obs;
  @override
  void onInit() {
    super.onInit();
    type.value = Get.parameters['type'] ?? (Get.arguments['type'] ?? '1');
    collectionId.value = Get.parameters['id'] ?? (Get.arguments['id'] ?? '0');
    url.value = Get.parameters['url'] ?? (Get.arguments['url'] ?? '');
    name.value = Get.parameters['name'] ?? (Get.arguments['name'] ?? '');
    getList();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        getList(loadMore: true);
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
  }

  // void confirmAction() {
  //   Get.toNamed(
  //     DanationConfirmWidget.path,
  //     arguments: {'type': type.value, 'chooseList': chooseList},
  //   );

  // }

  // void allChooseAction() {
  //   if (chooseList.isNotEmpty) {
  //     chooseList.clear();
  //   }
  //   for (GoodsModel item in dataList) {
  //     item.isChoose.value == true;
  //     chooseList.add(item);
  //   }

  //   number.value = dataList.length;
  //   bottomEnble.value = (chooseList.isNotEmpty);
  // }

  void updateNumberAction() {
    num.value = 0;
    for (var element in transferList) {
      if (element.isSelect == true) {
        num.value++;
      }
    }
    drawerCount.value++;
  }

  getList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<CollectionCanTransferPageModel>(
        ProductApi.userCollectionCanTransferPage,
        params: {
          'pageNum': pageNum,
          'pageSize': pageSize,
          'collectionId': collectionId.value,
          'platform': type.value == '1' ? 'INTERNAL' : 'EXTERNAL',
        },
        success: (result) {
          List<CollectionCanTransferPageItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            transferList.clear();
          }
          transferList.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  selectAll() {
    selectedList.value = [];
    var hasLength = 0;
    var maxCount = 19;
    var count = 0;
    for (var element in transferList) {
      if (element.isSelect == true) {
        if (hasLength < 20) {
          hasLength += 1;
        } else {
          showToast('超出最大值20');
          drawerCount.value++;
          return;
        }
      }
    }
    maxCount = maxCount - hasLength;
    var iterator = transferList.iterator;
    drawerCount.value++;
    while (iterator.moveNext()) {
      var item = iterator.current;
      if (item.isSelect == false) {
        if (maxCount < count) {
          drawerCount.value++;
          // return;
        } else {
          item.isSelect = true;
          count++;
             drawerCount.value++;
        }
      }
    }
    var chooseNum = 0;
    for (var element in transferList) {
      if (element.isSelect == true) {
        chooseNum++;
      }
    }
    num.value = chooseNum;
    drawerCount.value++;
  }
}
